package com.niu1078.joe.delegates;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.niu1078.joe.activitys.ProxyActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * author :ywq .
 * time: 2017/12/18:18:34.
 * desc :基类Fragment
 * action:这是一个单activity的app
 * 这是所有fragment的基类
 * 继承me.yokeyword.fragmentation_swipeback 下的SwipeBackFragment
 */
@SuppressWarnings("all")
public abstract class BaseDelegate extends SwipeBackFragment {

    //使用 butterknife
    private Unbinder mUnbinder = null;

    /**
     * 提供一个可以让子类传递view 或者是布局id的方法
     */
    public abstract Object setLayout();

    public abstract void onBindView(@Nullable Bundle savedInstanceState, View rootView);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final View rootView;

        if (setLayout() instanceof Integer) {
            rootView = inflater.inflate((Integer) setLayout(), container, false);
        } else if (setLayout() instanceof View) {
            rootView = (View) setLayout();
        } else {
            throw new RuntimeException(getClass().getName() + "setLayout()方法中传入的视图不是View或者布局id!!");
        }

        //butterKnife 绑定
        mUnbinder = ButterKnife.bind(this, rootView);
        onBindView(savedInstanceState, rootView);

        return rootView;
    }

    /**
     * 获取当前的activity对象
     * @return ProxyActivity
     */
    public final ProxyActivity getProxyActivity() {
        return (ProxyActivity) _mActivity;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //解除绑定
        if (mUnbinder != null) {

            mUnbinder.unbind();
        }
    }


}
